﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.Unity;

using Infrastructure.Web.ActionResults;
using System.Text;
using Skynet.BusinessEntities;
using Skynet.BusinessLogic.BusinessComponents;
using Infrastructure.WebMvc.MvcAttributes;
namespace Skynet.MVCWebSite.Areas.Administration.Controllers
{
    
    public class MgmCategoriesController : AsyncController
    {
        [Dependency]
        public IUnityContainer Container { get; set; }

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExams BCExams { get; set; }

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExamQueries BCExamQueries { get; set; }

        [Dependency]
        public Infrastructure.Aspect.Services.Traslator.IEntityTranslatorService EntityTranslatorService { get; set; }
        
        //[OutputCache(Duration = 100, VaryByParam = "*")]
        
        protected override IActionInvoker CreateActionInvoker()
        {
            return new Infrastructure.WebMvc.ActionInvoker.UnityActionInvoker(Container);
        }

        public void IndexAsync()
        {
            var categories = this.BCExamQueries.SearchExamCategories();
            AsyncManager.Parameters["data"] = categories;
            AsyncManager.OutstandingOperations.Decrement();
            
        }
        public ActionResult IndexCompleted(IEnumerable<ExamCategory> data)
        {
            return View(data);  
        }
        [RuleAuthorize]
        [ServerValidate("www.google.com")]
        public ActionResult List()
        {
            var categories = this.BCExamQueries.SearchExamCategories();
            return View(categories);
 
        }
        public ActionResult Details(Guid Id)
        {
            var category = (this.BCExamQueries.SearchExamCategories(  SearchExamCategoriesOptions.GetOne ,  Id)).FirstOrDefault();
            return View(category);  
        }
        public ActionResult Categories(string name)
        {
            
            var categories = this.BCExamQueries.SearchExamCategories();
            if (!string.IsNullOrWhiteSpace(name))
                categories = categories.Where(C => C.Title == name).ToList();          
            return View(categories);
        }

        public ActionResult CategoriesAsync()
        {
            var categories = this.BCExamQueries.SearchExamCategories();
            return View(categories);  
        }

        public string CategoriesString()
        {
            System.Threading.Thread.Sleep(10000);
            var categories = this.BCExamQueries.SearchExamCategories();
            var query = from c in categories select "<br/><b>" + c.Description + "</b>";
            StringBuilder text = new StringBuilder();
            foreach (var item in query)
            {
                text.Append(item);                
            }
            return text.ToString();
        }        
        public ActionResult CategoriesJson(string name)
        {
            System.Threading.Thread.Sleep(10000);
            var categories = this.BCExamQueries.SearchExamCategories();
            if (!string.IsNullOrWhiteSpace(name))
                categories = categories.Where(C => C.Title == name).ToList();
            return new JsonResult() { Data = categories, JsonRequestBehavior =  JsonRequestBehavior.AllowGet};
        }


        public ActionResult AjaxViewSample()
        {
            return this.View();
        }

        [ChildActionOnly]
        public ActionResult Category(ExamCategory item)
        {
            return View(item);
        }


        //[ValidateInput(true)]
        public ActionResult Create(ExamCategory category)
        {
            if (ModelState.IsValid)
            {
                category.Id = Guid.NewGuid();
                category.MarkAsAdded();
                this.BCExams.MaintenanceExamCategories(new List<ExamCategory>() { category });
                return this.RedirectToAction("Index");  
            }
            return View(); 
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Delete(Guid Id)
        {
            var category = (from c in this.BCExamQueries.SearchExamCategories() where c.Id == Id select c).FirstOrDefault();
            return View(category);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(ExamCategory category)
        {
            if (ModelState.IsValid)
            {
                category.MarkAsDeleted();
                this.BCExams.MaintenanceExamCategories(new List<ExamCategory>() { category });
                return this.RedirectToAction("Index");
            }
            else {
                return View();
            }
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(Guid Id)
        {
            var category = (from c in this.BCExamQueries.SearchExamCategories() where c.Id == Id select c).FirstOrDefault();
            return View(category);
        }        
        
        //[ValidateAntiForgeryToken]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(ExamCategory category)
        {
            if (ModelState.IsValid)
            {                
                category.MarkAsModified();
                this.BCExams.MaintenanceExamCategories(new List<ExamCategory>(){category}); 
                return RedirectToAction("Index");                  
            }
            return View(category);            
        }
       
        public ActionResult Logout()
        {
            var redirect = RedirectToAction("Index", "Default");
            return new LogoutActionResult(redirect);
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);
        }
        protected override void HandleUnknownAction(string actionName)
        {
            
            string format = "Action <b>[{0}].[{1}]</b> is not supported.";
            this.ViewData["Message"] = String.Format(format,this.RouteData.GetRequiredString("controller"), actionName);            
            //this.Response.StatusCode = 404;            
            this.View("Error").ExecuteResult(this.ControllerContext);
        }

    }
}
